// #include "../include/hashDataModel.h"
#include "hashDataModel.h"

HashDataModel::HashDataModel()
    :DataModel()
{
    initCommandMap();
}
HashDataModel::~HashDataModel(){

}

std::string HashDataModel::executeCommand(const std::string& command, std::istringstream& iss)
{
    if(commandMap.count(command)){
        std::cout << "Executing command: " << command << " ";
        return commandMap[command](iss);
    }
    return "error Hash " + command + " not found";
}

void HashDataModel::initCommandMap()
{
    commandMap["hash"] = [this](std::istringstream& iss) -> std::string {
        std::string word;
        while (iss >> word) {
            std::string key(std::move(word));
            iss >> word;
            std::string value(std::move(word));
            // std::cout << "Setting key: " << key << " value: " << value << std::endl;
            hashDataModel[key] = value;
        }
        return "OK";
    };
    commandMap["hget"] = [this](std::istringstream& iss) -> std::string {
        std::string key;
        iss >> key;
        if(hashDataModel.count(key)){
            return hashDataModel[key];
        }
        return "error No key: " + key + "  was found";
    };
    commandMap["hgetall"] = [this](std::istringstream& iss) -> std::string {
        std::string result;
        for(auto& key : hashDataModel){
            result += key.first + " " + key.second + "\n";
        }
        return result;
    };
}

std::vector<std::string> HashDataModel::getAllKeys(){
    std::vector<std::string> result;
    for(auto& key : hashDataModel){
        result.push_back(key.first);
        result.push_back(key.second);
    }
    return result;
}